package com.baizhi.ccpx;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.32.99");
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        System.out.println("等待接收消息。。。。。");
        /**
         * 消费者消费消息
         * 1.消费哪个队列
         * 2.消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
         * 3.消费者未成功消费的回调
         */
        channel.basicConsume("caipx",true,
                (s, delivery) -> {   //成功后应答
                     byte[] body = delivery.getBody();
                     String message=new String(body);
                    System.out.println(message);
                },
                (s)->{   // 失败后应答
                    System.out.println(s);
                });

        }
        }


